/// 首页
part of pda.page;

class PageHome extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PageHomeState();
  }
}

class _PageHomeState extends State<PageHome> {
  Map<String, MenuItem> menuItems = {};

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        appBar: AppBar(
          title: Text('首页'),
        ),
        body: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: buildMenuItems(),
          ),
        ),
      ),
      onWillPop: () async {
        return false;
      },
    );
  }

  List<Widget> buildMenuItems() {
    if (menuItems.isEmpty) {
      return [
        FutureBuilder(
          future: DefaultAssetBundle.of(context)
              .loadString('assets/data/home.yaml'),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              var yaml = loadYaml(snapshot.data);
              menuItems = MenuItem.fromYamlList(yaml);
              List<Widget> widgets = [];
              for (var item in menuItems.values) {
                if (item.visible) {
                  widgets.add(FlatButton(
                    padding: EdgeInsets.all(8.0),
                    onPressed: () {
                      onItemPressed(item);
                    },
                    child: Column(
                      children: <Widget>[
                        Image.asset(item.img),
                        Text(item.text)
                      ],
                    ),
                  ));
                }
              }
              if (widgets.isEmpty) {
                widgets.add(Opacity(opacity: 0));
              }
              return Wrap(
                alignment: WrapAlignment.center,
                spacing: 0.0,
                runSpacing: 0.0,
                children: widgets,
              );
            } else {
              return Opacity(
                opacity: 0,
              );
            }
          },
        )
      ];
    } else {
      List<Widget> widgets = [];
      for (var item in menuItems.values) {
        if (item.visible) {
          widgets.add(FlatButton(
            padding: EdgeInsets.all(8.0),
            onPressed: () {
              onItemPressed(item);
            },
            child: Column(
              children: <Widget>[Image.asset(item.img), Text(item.text)],
            ),
          ));
        }
        if (widgets.isEmpty) {
          widgets.add(Opacity(opacity: 0));
        }
      }
      return [
        Wrap(
          alignment: WrapAlignment.center,
          spacing: 0.0,
          runSpacing: 0.0,
          children: widgets,
        )
      ];
    }
  }

  onItemPressed(MenuItem item) {
    if (item.to != null) {
      try {
        Navigator.pushNamed(context, item.to);
      } catch (e) {
        showError(context, '${item.to}页面无效!');
      }
    } else {
      showAlert(context, '${item.id}对应的功能未实现!');
    }
  }
}
